package controller

import (
	"github.com/gin-gonic/gin"
	"go-proxy/admin/vo"
	"go-proxy/common"
	"go-proxy/config"
	"go-proxy/proxy"
)

func RestartServer(c *gin.Context) {
	name, ok := c.GetQuery("name")
	common.LogInfo("重启代理服务：", name)
	if !ok {
		vo.BizError(c, "name不能为空")
		return
	}
	proxy.HandleRestartServer(name)
	vo.OKMsg(c, "重启代理服务器成功")
}

func ServerList(c *gin.Context) {
	proxyConfig := config.ProxyConfigData.Proxy

	list := make([]vo.ServerInfo, 0)

	for _, server := range proxyConfig.Servers {
		list = append(list, vo.ServerInfo{
			Name:     server.Name,
			Port:     server.Port,
			Type:     server.Type,
			FileRoot: server.FileRoot,
			AuthName: server.AuthName,
			Status:   proxy.GetServerStatus(server.Name),
		})
	}
	vo.OKData(c, list)
}

func StopServer(c *gin.Context) {
	name, ok := c.GetQuery("name")
	if !ok {
		vo.BizError(c, "name不能为空")
		return
	}
	proxy.ShutdownServer(name)
	vo.OKMsg(c, "停止代理服务器成功")
}
